package cn.com.ddapp.ddm;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.ATMaddressBean;
import cn.com.ddapp.ddm.bean.AreaBean;
import cn.com.ddapp.ddm.bean.shopcar.ShopcarEntity;
import cn.com.ddapp.ddm.fragment.base.ClassificationFragment;
import cn.com.ddapp.ddm.fragment.base.FoundFragment;
import cn.com.ddapp.ddm.fragment.base.HomePagerFragment;
import cn.com.ddapp.ddm.fragment.base.LandHomePagerFragment;
import cn.com.ddapp.ddm.fragment.base.MineFragment;
import cn.com.ddapp.ddm.fragment.base.ShopCatFragment;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.CitysListActivity;
import cn.com.ddapp.ddm.ui.CommonScanActivity;
import cn.com.ddapp.ddm.ui.LoginActivity;
import cn.com.ddapp.ddm.ui.PermissionsActivity;
import cn.com.ddapp.ddm.ui.SearchActivity;
import cn.com.ddapp.ddm.ui.mine.InfoCenterActivity;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TDevice;
import cn.com.ddapp.ddm.util.ToastUtil;
import cn.com.ddapp.ddm.util.UpdateManager;

import static com.umeng.socialize.editorpage.ShareActivity.REQUEST_CODE;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView count;
    private TextView landcount;

    private ViewPager mViewPager;// 用来放置界面切换
    private FragmentAdapter mAdapter;
    private List<Fragment> fragments = new ArrayList<Fragment>();
    // 四个Tab，每个Tab包含一个按钮
    private LinearLayout tab1;
    private LinearLayout tab2;
    private LinearLayout tab3;
    private LinearLayout tab4;
    private LinearLayout tab5;
    // 四个按钮
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private ImageView iv4;
    private ImageView iv5;
    // 四个按钮
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private TextView tv5;
    private TextView islogintv;
    private LinearLayout cityLayout;
    private LinearLayout sousuLayout;
    private ImageView landscan;
    private ImageView land_xiao;
    private Intent intent;
    private LinearLayout loginlayout;
    private TextView cityland;
    private TextView tologout;

    private String address,userId;
    private Gson gson=new Gson();
    private int shopcarNum;
    int id;
    private String atmCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
//            setContentView(R.layout.activity_main);
//
//
//        }else if(this.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT){//竖屏
//            setContentView(R.layout.activity_main);
//            initView();
//            initDots();//显示购物车上小圆点的数量
//        }
        if(TDevice.isTablet(getApplicationContext())){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 横屏
        }else{
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        }
        setContentView(R.layout.activity_main);
        initView();

        initDots();//显示购物车上小圆点的数量
        AppConfig.TOMAIN="2";//跳转到首页的标志

        UpdateManager updateManager=UpdateManager.getUpdateManager();
        updateManager.checkAppUpdate(MainActivity.this, false);//检查版本
    }

    @Override
    protected void onStart() {
        super.onStart();
    }




    public void initDots() {
        userId = SPUtils.getStringData(MainActivity.this, SPUtils.USERID);
        if(!TextUtils.isEmpty(userId)){
            final OkUtilCallback okUtilCallback =  new OkUtilCallback() {
                @Override
                public void onMyResponse(String response, int id) {
                    ShopcarEntity entity = gson.fromJson(response,ShopcarEntity.class);
                    if((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))){
                        BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
                    }else{
                        shopcarNum=entity.getList().get(0).getGoodsList().size();
                        count.setText(shopcarNum+"");
                    }
                }
            };
            OKUtilAPI.post(OKUtilAPI.shopList, "userId=" + userId+"&addrId="+AppConfig.CITY_ID, okUtilCallback);
            islogintv.setText(SPUtils.getStringData(MainActivity.this,SPUtils.EMAIL));
            tologout.setVisibility(View.VISIBLE);
        }else{
            tologout.setVisibility(View.GONE);
            islogintv.setText("登录");
            count.setText(0+"");
        }

    }

    @Override
    protected void onResume() {
        super.onResume();

//        if(SPUtils.getBooleanData(this,SPUtils.ISLOGIN)){
//            loginlayout.setVisibility(View.INVISIBLE);
//        }
        initDots();
        String curId=AppConfig.TOMAIN;
        if (curId.equals("1")) {
            resetImgAndTV();
            iv4.setImageResource(R.mipmap.a_shoppingcart);
            tv4.setTextColor(getResources().getColor(R.color.color_orange));
            mViewPager.setCurrentItem(3);
        } else if (curId.equals("2")) {
            resetImgAndTV();
            iv1.setImageResource(R.mipmap.o_homepage);
            tv1.setTextColor(getResources().getColor(R.color.color_orange));
            mViewPager.setCurrentItem(0);
        }else if (curId.equals("3")) {
            resetImgAndTV();
            iv3.setImageResource(R.mipmap.orange_find);
            tv3.setTextColor(getResources().getColor(R.color.color_orange));
            mViewPager.setCurrentItem(2);
        }else if (curId.equals("4")) {
            resetImgAndTV();
            iv2.setImageResource(R.mipmap.o_classification);
            tv2.setTextColor(getResources().getColor(R.color.color_orange));
            mViewPager.setCurrentItem(1);
        }else if (curId.equals("5")) {
            resetImgAndTV();
            iv5.setImageResource(R.mipmap.wd);
            tv5.setTextColor(getResources().getColor(R.color.color_orange));
            mViewPager.setCurrentItem(4);
        }
        if(AppConfig.FIN.equals("2")){
            String cName=SPUtils.getStringData(this,SPUtils.AREANAME);
            cityland.setText(cName);
            System.out.println("cName=="+cName);
        }

    }



    HomePagerFragment homePagerFragment;
    private void initView() {
        cityLayout = ((LinearLayout) this.findViewById(R.id.city_layout));
        cityland = (TextView) this.findViewById(R.id.city_land);
        cityLayout.setOnClickListener(this);
        sousuLayout = ((LinearLayout) this.findViewById(R.id.sousuLayout));
        sousuLayout.setOnClickListener(this);
        landscan = ((ImageView) this.findViewById(R.id.land_scan));
        landscan.setOnClickListener(this);
        land_xiao = ((ImageView) this.findViewById(R.id.land_xiao));
        land_xiao.setOnClickListener(this);

        loginlayout = ((LinearLayout) this.findViewById(R.id.loginlayout));
        loginlayout.setOnClickListener(this);

//        cityLayout = ((LinearLayout) this.findViewById(R.id.city_layout));
//        cityLayout.setOnClickListener(this);
        sousuLayout = ((LinearLayout) this.findViewById(R.id.sousuLayout));
        sousuLayout.setOnClickListener(this);
        landscan = ((ImageView) this.findViewById(R.id.land_scan));
        landscan.setOnClickListener(this);

        loginlayout = ((LinearLayout) this.findViewById(R.id.loginlayout));
        loginlayout.setOnClickListener(this);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        count = (TextView) this.findViewById(R.id.count);
        tologout = (TextView) this.findViewById(R.id.tologout);
        tologout.setOnClickListener(this);
        if(SPUtils.getBooleanData(this,SPUtils.ISLOGIN)){
            tologout.setVisibility(View.VISIBLE);
        }else{
            tologout.setVisibility(View.GONE);
        }
            togetATMcode();
        iv1 = (ImageView) findViewById(R.id.iv1);
        iv2 = (ImageView) findViewById(R.id.iv2);
        iv3 = (ImageView) findViewById(R.id.iv3);
        iv4 = (ImageView) findViewById(R.id.iv4);
        iv5 = (ImageView) findViewById(R.id.iv5);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        tv4 = (TextView) findViewById(R.id.tv4);
        tv5 = (TextView) findViewById(R.id.tv5);
        islogintv = (TextView) findViewById(R.id.islogintv);

        tab1 = (LinearLayout) findViewById(R.id.tab1);
        tab2 = (LinearLayout) findViewById(R.id.tab2);
        tab3 = (LinearLayout) findViewById(R.id.tab3);
        tab4 = (LinearLayout) findViewById(R.id.tab4);
        tab5 = (LinearLayout) findViewById(R.id.tab5);
        homePagerFragment = new HomePagerFragment();
        if(!TDevice.isTablet(BaseApplication.getIntstance())){
            fragments.add(homePagerFragment);
        }else {
            fragments.add(new LandHomePagerFragment());
        }
        fragments.add(new ClassificationFragment());
        fragments.add(new FoundFragment());
        fragments.add(new ShopCatFragment());
        fragments.add(new MineFragment());

        /**
         * 初始化Adapter
         */
        mAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);

        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(fragments.size() - 1);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /**
             *ViewPage左右滑动时
             */
            @Override
            public void onPageSelected(int arg0) {
                int currentItem = mViewPager.getCurrentItem();
                switch (currentItem) {
                    case 0:
                        resetImgAndTV();
                        iv1.setImageResource(R.mipmap.o_homepage);
                        tv1.setTextColor(getResources().getColor(R.color.color_orange));
                        AppConfig.TOMAIN="2";
                        break;
                    case 1:
                        resetImgAndTV();
                        iv2.setImageResource(R.mipmap.o_classification);
                        tv2.setTextColor(getResources().getColor(R.color.color_orange));
                        AppConfig.TOMAIN="4";
                        break;
                    case 2:
                        resetImgAndTV();
                        iv3.setImageResource(R.mipmap.orange_find);
                        tv3.setTextColor(getResources().getColor(R.color.color_orange));
                        AppConfig.TOMAIN="3";
                        break;
                    case 3:
                        if (SPUtils.getBooleanData(MainActivity.this, SPUtils.ISLOGIN)) {
                            resetImgAndTV();
                            iv4.setImageResource(R.mipmap.a_shoppingcart);
                            tv4.setTextColor(getResources().getColor(R.color.color_orange));
                            AppConfig.TOMAIN="1";
                        } else {
                            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                            startActivityForResult(intent, AppConfig.MAIN_TAB_3);
                        }
                        break;
                    case 4:
                        resetImgAndTV();
                        iv5.setImageResource(R.mipmap.wd);
                        tv5.setTextColor(getResources().getColor(R.color.color_orange));
                        AppConfig.TOMAIN="5";
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

        id = getIntent().getIntExtra("id", 0);
        Log.i("aaa","--di--"+id);
    }

    /**
     * 根据ATM编码获取地址ID
     */
    private void togetATMcode() {
        atmCode=SPUtils.getStringData(MainActivity.this,SPUtils.ATMCODE);
//        if(atmCode.equals("")){
            try {
                StringBuffer sb = new StringBuffer();
                File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/SocialSecurityATM/atmCode.txt");
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line = "";
                while((line = br.readLine())!=null){
                    sb.append(line);
                }
                br.close();
                atmCode=sb.toString();
                if(TextUtils.isEmpty(atmCode)){
                    cityland.setText("获取地址失败\n请手动选择...");
                }else{
                    SPUtils.setStringData(MainActivity.this,SPUtils.ATMCODE,atmCode);
                    requestAddid(atmCode);
                }
                Log.i("aaa","---code--"+atmCode);

            }catch (Exception e){
                e.printStackTrace();
                cityland.setText("获取地址失败\n请手动选择...");
            }
//        }else{
//           cityland.setText(SPUtils.getStringData(MainActivity.this,SPUtils.AREANAME));
//        }

    }

    /**
     * @param atmCode
     * atm编码
     */
    private void requestAddid(String atmCode) {
        final OkUtilCallback okUtilCallback = new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                Log.i("aaa","---code--"+response.toString());
                System.out.println("---code--"+response.toString());
                ATMaddressBean bean=gson.fromJson(response,ATMaddressBean.class);
                if(bean.getResult().equals("OK")){
                    AppConfig.CITY_ID=bean.getArea().getId();
                    AppConfig.WID=bean.getArea().getId();
                    SPUtils.setStringData(MainActivity.this,SPUtils.AREANAME,bean.getArea().getName());
                    cityland.setText(bean.getArea().getName());
                    AppConfig.FIN="2";

                }else{
                    ToastUtil.show(getApplicationContext(),bean.getResult(),3500);
                }
            }

        };
        String params="atmNum="+atmCode;
        OKUtilAPI.post(OKUtilAPI.ATMCODE, params, okUtilCallback);
//        OkHttpUtils.post().url(OKUtilAPI.baseUrl+OKUtilAPI.ATMCODE)
//                .addParams("atmNum",atmCode)
//                .build()
//                .execute(new OkUtilCallback() {
//            @Override
//            public void onMyResponse(String response, int id) {
//                Log.i("aaa","---code--"+response.toString());
//                System.out.println("---code--"+response.toString());
//                ATMaddressBean bean=gson.fromJson(response,ATMaddressBean.class);
//                if(bean.getResult().equals("OK")){
//                        AppConfig.CITY_ID=bean.getArea().getId();
//                        AppConfig.WID=bean.getArea().getId();
//                        SPUtils.setStringData(MainActivity.this,SPUtils.AREANAME,bean.getArea().getName());
//                        cityland.setText(bean.getArea().getName());
//                        AppConfig.FIN="2";
//
//                }else{
//                    ToastUtil.show(getApplicationContext(),bean.getResult(),3500);
//                }
//            }
//        });
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.city_layout:
                intent=new Intent(MainActivity.this, CitysListActivity.class);
                startActivity(intent);
                break;
            case R.id.sousuLayout:
                intent=new Intent(MainActivity.this, SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.land_scan:
                intent=new Intent(MainActivity.this,CommonScanActivity.class);
                startActivity(intent);
                break;
            case R.id.loginlayout:
                if(SPUtils.getBooleanData(this,SPUtils.ISLOGIN)){
                    showMinePageFragment();
                }else{
                    intent=new Intent(MainActivity.this,LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.land_xiao:
                intent = new Intent(MainActivity.this, InfoCenterActivity.class);
                startActivity(intent);
                break;
            case R.id.tologout:
                SPUtils.setStringData(MainActivity.this, SPUtils.USERID, "");
                SPUtils.setBooleanData(MainActivity.this, SPUtils.ISLOGIN, false);
                SPUtils.setStringData(MainActivity.this, SPUtils.EMAIL, "");
                resetImgAndTV();
                iv1.setImageResource(R.mipmap.o_homepage);
                tv1.setTextColor(getResources().getColor(R.color.color_orange));
                mViewPager.setCurrentItem(0);
                initDots();
                BaseApplication.showToast("退出登录");
                Intent a=new Intent();
                a.setAction("logout");
                sendBroadcast(a);
                break;
        }
    }

//    @Override
//    public void onLocationChanged(AMapLocation amapLocation) {
//        if (mListener != null && amapLocation != null) {
//            if (amapLocation != null
//                    && amapLocation.getErrorCode() == 0) {
//                double lat=amapLocation.getLatitude();
//                double log=amapLocation.getLongitude();
//                Log.i("aaa","---latt--"+lat+"--"+log);
//                AppConfig.LAT=lat;
//                AppConfig.LNG=log;
//            } else {
//            }
//        }
//    }

    /**
     * @param lat
     * @param log
     * 根据定位获取经纬度
     */
    private void toLocation(double lat, double log) {
        final OkUtilCallback okUtilCallback = new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                AreaBean bean = gson.fromJson(response, AreaBean.class);
                if(!bean.getResult().equals("OK")){
                    BaseApplication.showToast(bean.getResult());
                }else{
                    AreaBean.IndexArea a=bean.getArea();
                    AppConfig.CITY_ID=a.getId();
                    SPUtils.setStringData(MainActivity.this,SPUtils.AREANAME,a.getName());
                    cityland.setText(a.getName());
                    AppConfig.FIN="2";
                }
            }

        };
        String params="longitude="+log+"&latitude="+lat;
        OKUtilAPI.post(OKUtilAPI.indexaddId, params, okUtilCallback);
    }

//    @Override
//    public void activate(OnLocationChangedListener listener) {
//        mListener = listener;
//        if (mlocationClient == null) {
//            mlocationClient = new AMapLocationClient(getApplicationContext());
//            mLocationOption = new AMapLocationClientOption();
//            //设置定位监听
//            mlocationClient.setLocationListener(this);
//            //设置为高精度定位模式
//            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//            mLocationOption.setInterval(900000);//设置定位间隔,单位毫秒,默认为2000ms
//            //设置定位参数
//            mlocationClient.setLocationOption(mLocationOption);
//            mlocationClient.startLocation();
////            address= mlocationClient.getLastKnownLocation().getCountry()+mlocationClient.getLastKnownLocation().getProvince()+mlocationClient.getLastKnownLocation().getCity()+mlocationClient.getLastKnownLocation().getDistrict();
////            Log.i("yu","--address-"+address);
//            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
//            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
//            // 在定位结束后，在合适的生命周期调用onDestroy()方法
//            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
//
//        }
//    }

//    @Override
//    public void deactivate() {
//        mListener = null;
//        if (mlocationClient != null) {
//            mlocationClient.stopLocation();
//            mlocationClient.onDestroy();
//        }
//        mlocationClient = null;
//    }


    /**
     * 功能：主页引导栏的三个Fragment页面设置适配器
     */
    public class FragmentAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragments;

        public FragmentAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        public Fragment getItem(int fragment) {
            return fragments.get(fragment);
        }

        public int getCount() {
            return fragments.size();
        }
    }

    public void tabClick(View view) {
        switch (view.getId()) {
            case R.id.tab1:
            case R.id.tv1:
                resetImgAndTV();
                mViewPager.setCurrentItem(0);
                iv1.setImageResource(R.mipmap.o_homepage);
                tv1.setTextColor(getResources().getColor(R.color.color_orange));
                Intent a1=new Intent();
                a1.setAction("update");
                sendBroadcast(a1);
                AppConfig.TOMAIN="2";
                break;
            case R.id.tab2:
            case R.id.tv2:
                resetImgAndTV();
                mViewPager.setCurrentItem(1);
                iv2.setImageResource(R.mipmap.o_classification);
                tv2.setTextColor(getResources().getColor(R.color.color_orange));
                AppConfig.TOMAIN="4";
                break;
            case R.id.tab3:
            case R.id.tv3:
                    resetImgAndTV();
                    mViewPager.setCurrentItem(2);
                    iv3.setImageResource(R.mipmap.orange_find);
                    tv3.setTextColor(getResources().getColor(R.color.color_orange));
//                if (SPUtils.getBooleanData(MainActivity.this, SPUtils.ISLOGIN)) {
//
//
//                }else {
//                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
//                    startActivityForResult(intent, AppConfig.MAIN_TAB_2);
//                }
                AppConfig.TOMAIN="3";
                break;
            case R.id.tab4:
            case R.id.tv4:

                if (SPUtils.getBooleanData(MainActivity.this, SPUtils.ISLOGIN)) {
                    resetImgAndTV();
                    mViewPager.setCurrentItem(3);
                    iv4.setImageResource(R.mipmap.a_shoppingcart);
                    tv4.setTextColor(getResources().getColor(R.color.color_orange));
                    AppConfig.TOMAIN="1";
                } else {
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivityForResult(intent, AppConfig.MAIN_TAB_3);
                }


                break;
            case R.id.tab5:
            case R.id.tv5:
                /*if (SPUtils.getBooleanData(MainActivity.this, SPUtils.ISLOGIN)) {
                    resetImgAndTV();
                    mViewPager.setCurrentItem(4);
                    iv5.setImageResource(R.mipmap.wd);
                    tv5.setTextColor(getResources().getColor(R.color.color_orange));
                } else {
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivityForResult(intent, AppConfig.MAIN_TAB_4);
                }*/
                resetImgAndTV();
                mViewPager.setCurrentItem(4);
                iv5.setImageResource(R.mipmap.wd);
                tv5.setTextColor(getResources().getColor(R.color.color_orange));

                Intent a=new Intent();
                a.setAction("again");
                sendBroadcast(a);
                AppConfig.TOMAIN="5";
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 0x86){
            switch (requestCode){
//                case AppConfig.MAIN_TAB_2:
//                    mViewPager.setCurrentItem(2);
//                    break;
                case AppConfig.MAIN_TAB_3:
                    mViewPager.setCurrentItem(3);
                    break;
//                case AppConfig.MAIN_TAB_4:
//                    mViewPager.setCurrentItem(4);
//                    break;


            }
        }
        if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
            homePagerFragment.onActivityResult(requestCode,resultCode,data);
        }else if(requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_GRANTED){
            try {
                homePagerFragment.onActivityResult(requestCode,resultCode,data);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 跳到首页
     */
    public void showHomePageFragment() {
        mViewPager.setCurrentItem(0);
    }

    /**
     * 跳到首分类
     */
    public void showSortPageFragment() {
        mViewPager.setCurrentItem(1);
    }

    /**
     * 我的
     */
    public void showMinePageFragment() {
        mViewPager.setCurrentItem(4);
    }

    /**
     * 把所有图片变暗
     */
    private void resetImgAndTV() {
        iv1.setImageResource(R.mipmap.b_home_page);
        iv2.setImageResource(R.mipmap.b_classification);
        iv3.setImageResource(R.mipmap.b_find);
        iv4.setImageResource(R.mipmap.b_shoppingcart);
        iv5.setImageResource(R.mipmap.b_people);
        tv1.setTextColor(getResources().getColor(R.color.action_color));
        tv2.setTextColor(getResources().getColor(R.color.action_color));
        tv3.setTextColor(getResources().getColor(R.color.action_color));
        tv4.setTextColor(getResources().getColor(R.color.action_color));
        tv5.setTextColor(getResources().getColor(R.color.action_color));
    }
    private long exitTime = 0;
    @Override
    public void onBackPressed() {
        if(System.currentTimeMillis() - exitTime > 2000) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
}
